package com.zhq.blog.core.domain.money;


import java.util.List;

import javax.persistence.Entity;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.zhq.blog.core.domain.BasePersistentDomain;
import com.zhq.blog.core.domain.search.CreateCardSearch;

@Entity
public class CreateCard extends BasePersistentDomain<CreateCardRepository> {
	
	private String card_num;   //充值卡名字名字
	private String card_password;   //充值卡图片
	private long cardId;//充值卡ID
	private long businessManId;//商户ID
	private long status;//卡号状态 已充值  未充值 已售出
	
	
	public CreateCard(){}

	public CreateCard(String card_num, String card_password, long cardId,
			long businessManId, long status) {
		super();
		this.card_num = card_num;
		this.card_password = card_password;
		this.cardId = cardId;
		this.businessManId = businessManId;
		this.status = status;
	}



	public String getCard_num() {
		return card_num;
	}


	public void setCard_num(String card_num) {
		this.card_num = card_num;
	}


	public String getCard_password() {
		return card_password;
	}


	public void setCard_password(String card_password) {
		this.card_password = card_password;
	}


	public long getCardId() {
		return cardId;
	}


	public void setCardId(long cardId) {
		this.cardId = cardId;
	}


	public long getBusinessManId() {
		return businessManId;
	}


	public void setBusinessManId(long businessManId) {
		this.businessManId = businessManId;
	}


	public long getStatus() {
		return status;
	}


	public void setStatus(long status) {
		this.status = status;
	}
	
	

	public  Specification<CreateCard> selectCondition(final CreateCardSearch createCardSearch) {
		return new Specification<CreateCard>() {
			@Override
			public Predicate toPredicate(Root<CreateCard> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				
				expressions.add(builder.equal(root.<Boolean> get("deleted"),0));

				if (StringUtils.isNotBlank(createCardSearch.getCard_num())) {
					expressions.add(builder.or( builder.like(root.<String> get("card_num"), "%"+ createCardSearch.getCard_num() + "%")));

					
				}
				if (StringUtils.isNotBlank(createCardSearch.getCardId())) {
					expressions.add(builder.equal(root.<Long> get("cardId"),createCardSearch.getCardId())); // 品牌id

					
				}
				if (StringUtils.isNotBlank(createCardSearch.getStatus())) {
					expressions.add(builder.equal(root.<Long> get("status"),createCardSearch.getStatus())); // 品牌id

					
				}

				return query.where(predicate).getRestriction();

			}
		};
	}
	
	
	
}
